package com.zenithmind.coding.controller;

import com.zenithmind.common.result.Result;
import com.zenithmind.coding.controller.base.BaseController;
import com.zenithmind.coding.pojo.vo.ProblemVO;
import com.zenithmind.coding.service.ProblemFavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 问题收藏控制器 - 遵循单一职责原则，专门处理问题收藏相关功能
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "问题收藏", description = "编程问题收藏管理相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/problems/favorites")
@RequiredArgsConstructor
public class ProblemFavoriteController extends BaseController {

    private final ProblemFavoriteService problemFavoriteService;

    @Operation(summary = "收藏问题", description = "收藏指定编程问题")
    @PostMapping("/{id}")
    public Result<Boolean> favoriteProblem(@Parameter(description = "问题ID") @PathVariable String id) {
        validateId(id, "问题ID");
        String userId = getCurrentUserId();
        Boolean result = problemFavoriteService.favoriteProblem(id, userId);
        logOperation("收藏问题", "问题ID: " + id);
        return success(result);
    }

    @Operation(summary = "取消收藏问题", description = "取消收藏指定编程问题")
    @DeleteMapping("/{id}")
    public Result<Boolean> unfavoriteProblem(@Parameter(description = "问题ID") @PathVariable String id) {
        validateId(id, "问题ID");
        String userId = getCurrentUserId();
        Boolean result = problemFavoriteService.unfavoriteProblem(id, userId);
        logOperation("取消收藏问题", "问题ID: " + id);
        return success(result);
    }

    @Operation(summary = "获取收藏的问题", description = "获取用户收藏的编程问题")
    @GetMapping
    public Result<List<ProblemVO>> getUserFavoriteProblems(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        String userId = getCurrentUserId();
        List<ProblemVO> problems = problemFavoriteService.getUserFavoriteProblems(userId, limit);
        return success(problems);
    }

    @Operation(summary = "检查收藏状态", description = "检查问题是否已被用户收藏")
    @GetMapping("/{id}/status")
    public Result<Boolean> checkFavoriteStatus(@Parameter(description = "问题ID") @PathVariable String id) {
        validateId(id, "问题ID");
        String userId = getCurrentUserId();
        Boolean isFavorited = problemFavoriteService.checkFavoriteStatus(id, userId);
        return success(isFavorited);
    }

    @Operation(summary = "批量收藏问题", description = "批量收藏多个编程问题")
    @PostMapping("/batch")
    public Result<Integer> batchFavoriteProblems(@RequestBody List<String> problemIds) {
        String userId = getCurrentUserId();
        Integer count = problemFavoriteService.batchFavoriteProblems(problemIds, userId);
        logOperation("批量收藏问题", "数量: " + count);
        return success(count);
    }

    @Operation(summary = "批量取消收藏", description = "批量取消收藏多个编程问题")
    @DeleteMapping("/batch")
    public Result<Integer> batchUnfavoriteProblems(@RequestBody List<String> problemIds) {
        String userId = getCurrentUserId();
        Integer count = problemFavoriteService.batchUnfavoriteProblems(problemIds, userId);
        logOperation("批量取消收藏问题", "数量: " + count);
        return success(count);
    }

    @Operation(summary = "获取收藏统计", description = "获取用户收藏统计信息")
    @GetMapping("/statistics")
    public Result<Object> getFavoriteStatistics() {
        String userId = getCurrentUserId();
        Object statistics = problemFavoriteService.getUserFavoriteStatistics(userId);
        return success(statistics);
    }

    @Operation(summary = "获取收藏分类", description = "获取用户收藏问题的分类统计")
    @GetMapping("/categories")
    public Result<Object> getFavoriteCategories() {
        String userId = getCurrentUserId();
        Object categories = problemFavoriteService.getUserFavoriteCategories(userId);
        return success(categories);
    }

    @Operation(summary = "获取收藏难度分布", description = "获取用户收藏问题的难度分布")
    @GetMapping("/difficulty-distribution")
    public Result<Object> getFavoriteDifficultyDistribution() {
        String userId = getCurrentUserId();
        Object distribution = problemFavoriteService.getUserFavoriteDifficultyDistribution(userId);
        return success(distribution);
    }

    @Operation(summary = "搜索收藏的问题", description = "在用户收藏的问题中搜索")
    @GetMapping("/search")
    public Result<List<ProblemVO>> searchFavoriteProblems(
            @Parameter(description = "关键词") @RequestParam String keyword,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        String userId = getCurrentUserId();
        List<ProblemVO> problems = problemFavoriteService.searchUserFavoriteProblems(userId, keyword, limit);
        return success(problems);
    }

    @Operation(summary = "按标签筛选收藏", description = "按标签筛选用户收藏的问题")
    @GetMapping("/filter/tags")
    public Result<List<ProblemVO>> filterFavoritesByTags(
            @Parameter(description = "标签列表") @RequestParam List<String> tags,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        String userId = getCurrentUserId();
        List<ProblemVO> problems = problemFavoriteService.filterUserFavoritesByTags(userId, tags, limit);
        return success(problems);
    }

    @Operation(summary = "按难度筛选收藏", description = "按难度筛选用户收藏的问题")
    @GetMapping("/filter/difficulty")
    public Result<List<ProblemVO>> filterFavoritesByDifficulty(
            @Parameter(description = "难度等级") @RequestParam Integer difficulty,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        String userId = getCurrentUserId();
        List<ProblemVO> problems = problemFavoriteService.filterUserFavoritesByDifficulty(userId, difficulty, limit);
        return success(problems);
    }

    @Operation(summary = "导出收藏列表", description = "导出用户收藏的问题列表")
    @PostMapping("/export")
    public Result<String> exportFavoriteProblems(
            @Parameter(description = "导出格式") @RequestParam(defaultValue = "json") String format) {
        String userId = getCurrentUserId();
        String exportData = problemFavoriteService.exportUserFavoriteProblems(userId, format);
        logOperation("导出收藏列表", "格式: " + format);
        return success(exportData);
    }
}
